package com.gui.musiceditor;

import com.data.MusicFile;
import com.effects.BasicEffect;
import com.gooddies.swing.hComboBox;
import com.gooddies.texteditors.DefaultTextField;
import com.swingson.SwingsonGuiBuilder;
import java.awt.Frame;
import javax.swing.JDialog;

/**
 * @author sad
 */
public class MusicFileEditorDialog extends JDialog {

    private final MusicFile music;
    private final DefaultTextField idTF = new DefaultTextField();
    private final hComboBox<BasicEffect> effectCB = new hComboBox<>();
    private final DefaultTextField mp3TF = new DefaultTextField();

    public MusicFileEditorDialog(Frame frame, MusicFile music) {
        super(frame, true);
        setTitle("Music File editor");
        setResizable(false);
        this.music = music;
        for (BasicEffect universe : BasicEffect.getAllEffects()) {
            effectCB.addItem(universe);
        }

        showMusicFile(music);
        SwingsonGuiBuilder.createGuiFromJsonInPackage(this);
    }

    private void showMusicFile(MusicFile musicFile) {
        idTF.setText(musicFile.getId());
        mp3TF.setText(musicFile.getFile());
        if (music.getEffectId() != null) {
            for (BasicEffect e : effectCB.iterable()) {
                if (e.getId().equals(music.getEffectId())) {
                    effectCB.setSelectedItem(e);
                    break;
                }
            }
        }
    }

    private void onSave() {
        if (idTF.getText().trim().isEmpty()) {
            idTF.showError("Please enter id");
            return;
        }
        String path = mp3TF.getText().trim();
        if (path.isEmpty()) {
            mp3TF.showError("Please enter music file");
            return;
        }

        music.setId(idTF.getText().trim());
        music.setFile(mp3TF.getText());
        music.setEffectId(effectCB.getSelectedItem().getId());
        setVisible(false);
        dispose();
    }

    private void onCancel() {
        setVisible(false);
        dispose();
    }
}
